<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Expeditions' );

$gameturn = getCurrentTurn();

$query = "SELECT count(*) FROM colony WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$colonies = $row[ 'count(*)' ];

$query = "SELECT count(*) FROM ruler_expedition WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND ( expedition_id = 13 OR expedition_id = 14 OR expedition_id = 15 ) AND completed = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$colonies += $row[ 'count(*)' ];

?>
					<div class="blok">
<?php
print_operations_tab( 'Expeditions' );
?>
					</div>
					<br />
<?php
printGlobalSoldiers( $_SESSION[ 'ruler_id' ] );
include( "../global/resources.php" );

$query = "SELECT count(*) " .
		"FROM ruler_expedition " .
		"WHERE completed = 0 AND ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);

if( $row[ 'count(*)' ] < getQueueLength() ) {
	$query = "SELECT * " .
			"FROM expeditions " .
			"JOIN upgrade_status ON expeditions.required = upgrade_id " .
			"WHERE required_level = level AND achieved = 1 " .
			"AND ruler_id = " . $_SESSION[ 'ruler_id' ] .
			" ORDER BY required_level, time_cost, name";
			
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	
	$i = 0;
	if( $num > 0 ) {
		$count = 0;
		print "<br /><form method=\"post\" action=\"que_expedition.php\" >\r\n";
		print "<table><th colspan=\"9\">" . translate( 'Available expeditions' ) . "</th>\r\n";
		print "<tr><td>" . translate( 'Select' ) . "</td><td>" . translate( 'Amount' ) . "</td><td>" . translate( 'Repeat' ) . "</td><td>" . translate( 'Name' ) . "</td><td align=\"right\">" . translate( 'Gold cost' ) . "</td><td align=\"right\">" . translate( 'Iron cost' ) . "</td><td align=\"right\">" . translate( 'Coal cost' ) . "</td><td align=\"right\">" . translate( 'Required men' ) . "</td><td align=\"right\">" . translate( 'Duration' ) . "</td></tr>\r\n";
		while ( $i < $num ) {
		    if( ( $colonies < 3 && mysql_result( $result, $i, "expedition_id" ) > 12 ) || ( mysql_result( $result, $i, "expedition_id" ) < 13 || mysql_result( $result, $i, "expedition_id" ) > 15 ) ) {
			    if( $count % 2 == 0 ) {
			        print "<tr class=\"even\">";
			    }
			    else {
			        print "<tr>";
			    }
		
				print( "<td><input type=\"checkbox\" onclick=\"fillField( this.name )\" control=\"expedition\" name=\"check_" . mysql_result( $result, $i, "expedition_id" ) . "\" /></td>\r\n" );
				print( "<td><input size=\"1\" type=\"text\" name=\"text_" . mysql_result( $result, $i, "expedition_id" ) . "\" id=\"" . mysql_result( $result, $i, "expedition_id" ) . "\" /></td>\r\n" );
				print( "<td><input type=\"checkbox\" control=\"expedition\" name=\"repeat_" . mysql_result( $result, $i, "expedition_id" ) . "\" /></td>\r\n"  );
				print( "<td><a href=\"expedition.php?id=" . mysql_result( $result, $i, "expedition_id" ) . "\">" . translate( mysql_result( $result, $i, "name" ) ) . "</a></td>\r\n"  );
				print( "<td align=\"right\"><strong class=\"gold\">" . number_format( mysql_result( $result, $i, "gold_cost" ) ) . "</strong></td>\r\n"  );
				print( "<td align=\"right\"><strong class=\"iron\">" . number_format( mysql_result( $result, $i, "iron_cost" ) ) . "</strong></td>\r\n"  );
				print( "<td align=\"right\"><strong class=\"coal\">" . number_format( mysql_result( $result, $i, "coal_cost" ) ) . "</strong></td>\r\n"  );
				print( "<td align=\"right\"><strong class=\"recruits\">" . number_format( mysql_result( $result, $i, "recruit_cost" ) ) . "</strong></td>\r\n"  );
				print( "<td align=\"right\">" . number_format( mysql_result( $result, $i, "time_cost" ) ) . " " . translate( 'days' ) . "</td>\r\n</tr>\r\n"  );
				$count ++;
		    }
			$i ++;
		}
		print "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td><input type=\"submit\" value=\"" . translate( 'Start expedition' ) . "\"/></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
		print "</table>\r\n</form><br />\r\n";
	}
	else {
		print( "<br /><div class=\"blok\">\r\n" );
		print( translate( 'You can\'t start any expedition at the moment.' ) . "<br />\r\n" );
		print( "</div><br />\r\n" );    
	}
}
else {
	print( "<br />\r\n" );
}

if( isset( $_SESSION[ 'que_expedition_status' ] ) ) {
	print "<div class=\"blok\">" . $_SESSION[ 'que_expedition_status' ] . "</div><br />\r\n";
	unset( $_SESSION[ 'que_expedition_status' ] );
}

$query = "SELECT * " .
		"FROM ruler_expedition " .
		"JOIN expeditions ON expeditions.expedition_id = ruler_expedition.expedition_id " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND completed = 0 " .
		"ORDER BY rank";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );

$i = 0;
if( $num > 0 ) {
	print "<table><th colspan=\"6\">" . translate( 'Expedition schedule' ) . "</th>\r\n";
	print "<tr><td>" . translate( 'Rank' ) . "</td><td>" . translate( 'Name' ) . "</td><td>" . translate( 'Repeat' ) . "</td><td>" . translate( 'Status' ) . "</td><td>" . translate( 'Remaining' ) . "</td><td>" . translate( 'Delete' ) . "</td></tr>\r\n";
	while ( $i < $num ) {
	    if( $i % 2 == 0 ) {
	        print "<tr id=\"even\">";
	    }
	    else {
	        print "<tr>";
	    }
		
		if( mysql_result( $result, $i, "rank" ) > 0 ) {
			print( "<td align=\"right\">" . mysql_result( $result, $i, "rank" ) . "</td>\r\n" );
		}
		else {
			print( "<td>&nbsp;</td>" );
		}
		print( "<td><a href=\"expedition.php?id=" . mysql_result( $result, $i, "expedition_id" ) . "\">" . translate( mysql_result( $result, $i, "name" ) ) . "</a></td>\r\n"  );
?>
					<td>
						<form method="post" action="set_expedition_repeat.php">
							<input type="hidden" name="ruler_expedition_id" value="<?php print( mysql_result( $result, $i, 'ruler_expedition_id' ) ); ?>" />
							<select onchange="this.parentNode.submit();" name="expedition-repeat">
								<option value="0"<?php ( mysql_result( $result, $i, 'repeat' ) == 0 ? print( ' selected="selected"' ) : print( '' ) ); ?>><?php transprint( 'Do not repeat' ); ?></option>
								<option value="1"<?php ( mysql_result( $result, $i, 'repeat' ) == 1 ? print( ' selected="selected"' ) : print( '' ) ); ?>><?php transprint( 'Repeat' ); ?></option>
							</select>
						</form>
					</td>
<?php
		
		if( mysql_result( $result, $i, "started" ) == 1 ) {
			$status = translate( 'underway' );
		}
		else if( mysql_result( $result, $i, "pending" ) == 1 ) {
			$status = translate( 'pending' );
		}
		else {
			$status = translate( 'scheduled' );
		}
		
		print( "<td>$status</td>" );
		
		if( mysql_result( $result, $i, "started" ) == 1 && mysql_result( $result, $i, "complete_turn" ) > $gameturn ) {
			print( "<td>" . number_format( mysql_result( $result, $i, "complete_turn" ) - $gameturn ) . " " . translate( 'days' ) . "</td>" );
		}
		else {
			print( "<td>&nbsp;</td>" );
		}
		if( mysql_result( $result, $i, "started" ) == 0 && mysql_result( $result, $i, "completed" ) == 0 ) {
			print( "<td><a href=\"delete_queue.php?id=" . mysql_result( $result, $i, "ruler_expedition_id" ) . "\"><img src=\"../resources/delete.png\" alt=\"" . translate( 'Remove from queue' ) . "\" title=\"" . translate( 'Remove from queue' ) . "\" border=\"0\" /></a></td>" );
		}
		else {
			print( "<td>&nbsp;</td>" );
		}
		print( "</tr>\r\n" );
		$i ++;
	}
	print "</table>\r\n";
}
else {
	print( "<div class=\"blok\">\r\n" );
	print( translate( 'No expeditions are queued at the moment.' ) . "<br />\r\n" );
	print( "</div>\r\n" );    
}

$query = "SELECT * FROM colony " .
		"JOIN colony_level ON colony.colony_level_id = colony_level.colony_level_id " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] .
		" ORDER BY name";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );

$i = 0;
if( $num > 0 ) {
	print "<br /><table><th colspan=\"4\">" . translate( 'Colonies' ) . "</th>\r\n";
	print "<tr><td>" . translate( 'Name' ) . "</td><td>" . translate( 'Population' ) . "</td><td>" . translate( 'Main resource' ) . "</td><td>" . translate( 'Next transport arrival' ) . "</td></tr>\r\n";
	while ( $i < $num ) {
	    if( $i % 2 == 0 ) {
	        print "<tr class=\"even\">";
	    }
	    else {
	        print "<tr>";
	    }
		
		print( "<td>" . translate( mysql_result( $result, $i, "colony_level.type_name" ) ) . ": " . "<a href=\"colony.php?id=" . mysql_result( $result, $i, "colony_id" ) . "\">" . mysql_result( $result, $i, "colony.name" ) . "</a></td>\r\n"  );
		print( "<td align=\"right\">" . number_format( mysql_result( $result, $i, "colony.population" ) ) . "</td>" );
		if( mysql_result( $result, $i, "type" ) == 0 ) {
			$type = "<strong class=\"gold\">" . translate( 'Gold' ) . "</strong>";
		}
		else if( mysql_result( $result, $i, "type" ) == 1 ) {
			$type = "<strong class=\"coal\">" . translate( 'Coal' ) . "</strong>";
		}
		else {
			$type = "<strong class=\"iron\">" . translate( 'Iron' ) . "</strong>";
		}
		print( "<td align=\"right\">" . $type . "</td>" );
		
		$query3 = "SELECT turn FROM colony_transport WHERE colony_id = " . mysql_result( $result, $i, "colony_id" ) . " ORDER BY turn LIMIT 1";
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC);
		if( isset( $row3[ 'turn' ] ) ) {
			$arrival_turn = $row3[ 'turn' ];
			if( $arrival_turn - $gameturn == 1 ) {
				print( "<td align=\"right\">" . translate( '1 day' ) . "</td>" );
			}
			else {
				print( "<td align=\"right\">" . number_format( $arrival_turn - $gameturn ) . " " . translate( 'days' ) . "</td>" );
			}
		}
		else {
			print( "<td>" . translate( 'No transport under way' ) . "</td>" );
		}
		print( "</tr>\r\n" );
		$i ++;
	}
	print "</table>\r\n";
}

$query = "SELECT * FROM ruler_expedition " .
		"JOIN expeditions ON expeditions.expedition_id = ruler_expedition.expedition_id " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND completed = 1" .
		" ORDER BY complete_turn DESC";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );

$i = 0;
if( $num > 0 ) {
	print "<br /><table><th colspan=\"2\">" . translate( 'Recently completed expeditions' ) . "</th>\r\n";
	print "<tr><td>" . translate( 'Name' ) . "</td><td>" . translate( 'Returned at' ) . "</td></tr>\r\n";
	while ( $i < $num ) {
	    if( $i % 2 == 0 ) {
	        print "<tr id=\"even\">";
	    }
	    else {
	        print "<tr>";
	    }
		
		print( "<td><a href=\"expedition.php?id=" . mysql_result( $result, $i, "expedition_id" ) . "\">" . translate( mysql_result( $result, $i, "name" ) ) . "</a></td>\r\n"  );
		print( "<td>" . mysql_result( $result, $i, "complete_date" ) . "</td>" );
		
		print( "</tr>\r\n" );
		$i ++;
	}
	print "</table>\r\n";
}
?>
				<script type="text/javascript">
				function fillField( name ) {
					var tmp = document.getElementById( name.replace( "check_", "" ) );
					if( tmp && ( !tmp.value || tmp.value == "" ) ) {
						tmp.setAttribute( "value", "1" );
					}
				}
				</script>
<?php
aos_footer();
?>